package com.zm.campus.controller;

import com.zm.campus.pojo.RespBean;
import com.zm.campus.pojo.RespBeanEnum;
import com.zm.campus.service.LoginService;
import com.zm.campus.service.impl.WebSocketServer;
import com.zm.campus.vo.LoginVo;
import com.zm.campus.vo.RegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;

import static com.zm.campus.service.impl.WebSocketServer.getOnlineCount;

@RestController
public class LoginController {


    @Autowired
    private LoginService loginService;


    //登录
    @PostMapping("/doLogin")
    public RespBean doLogin(@RequestBody LoginVo loginVo, HttpServletRequest request, HttpServletResponse response){//@Valid这个注解是spring-boot-starter-validation包提供的参数校验注解
            return  loginService.doLogin(loginVo,request,response);
    }

    //注册
    @PostMapping("/register")
    public RespBean register(@RequestBody RegisterVo registerVo){
        return loginService.register(registerVo);
    }
    //获取所有学校列表
    @RequestMapping("/getSchoolList")
    public RespBean getSchoolList(String userId){
        return  loginService.getSchoolList(userId);
    }
    //退出登录
    @RequestMapping("/loginOut")
    public RespBean loginOut(String userId,HttpServletResponse response,HttpServletRequest request){
        return loginService.loginOut(userId,response,request);
    }
    //获取指定用户的盐
    @PostMapping("/getUserSalt")
    public RespBean getUserSalt(String userId) throws UnsupportedEncodingException {//@Valid这个注解是spring-boot-starter-validation包提供的参数校验注解
        return  loginService.getUserSalt(userId);
    }
    //获取待登录用户的头像

    @PostMapping("/getLoginAvatar")
    public RespBean getLoginAvatar(String userId) {//@Valid这个注解是spring-boot-starter-validation包提供的参数校验注解
        return  loginService.getLoginAvatar(userId);
    }

    @GetMapping("/getOnlineNum")
    public RespBean getOnlineNum(){
        return RespBean.success(getOnlineCount());
    }
}
